<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  users
 */

class m_backoffice_actions_AjaxBoSearchAction extends m_backoffice_extends_Action
{
    public function execute()
    {
        $q = $this->getRequestParameter('q');
        $href = $this->getRequestParameter('moduleName');
        $routing = f_core_Routing::getInstance()->setRequestURL(substr($href,1));

        if(!is_null($this->getRequestParameter('configModule')))
        {
            $module = $this->getRequestParameter('configModule');
        }
        else
        {
            $module = $this->getRequest()->getParameter("module");
        }
        
        $docProvider = f_document_Provider::getInstance();

        $fPath = f_file_Resolver::getInstance()->getOverload(MODULES_DIR.DS.$module.DS.'config'.DS.'boSearch.xml');

        if(file_exists($fPath))
        {
            $xml = f_xml_Xpath::getInstance($fPath);

            $docs = array();

            $lang = $this->getRequestParameter('docLang', '*');

            foreach($xml->getElementsFromXpath('item') as $element)
            {
                $req = $docProvider->loadDocuments($element->getContent())->orLike('document_label', $q.'%');

                if(!is_null($element->getAttributeValue('fields')))
                {
                    $fields = explode(' ', $element->getAttributeValue('fields'));
                    if(count($fields) > 0)
                    {
                        foreach($fields as $field)
                        {
                            $req->orLike($field, $q.'%');
                        }
                    }
                }

                $res = $req->limit($this->getRequestParameter('limit', 10))->retrieveDocuments($lang);

                if(count($res)>0)
                {
                    foreach($res as $doc)
                    {
                        if($doc instanceof m_backoffice_documents_Folder)
                        {
                            if($doc->getModule() == $module)
                            {
                                $this->getItem($doc);
                            }
                        }
                        else
                        {
                            $this->getItem($doc);
                        }
                    }
                }
            }

            // First get from f_document
            if(preg_match('/^[0-9]+$/', $q) && $docProvider->checkDocument($q))
            {
                $doc = $docProvider->getByUniqueId($q);
                $this->getItem($doc);                
            }
        }
    }

    private function getItem($doc)
    {
        $docOrig = $doc;

        $directAccessModules = array('website', 'backoffice', 'users');

        if(!in_array($doc->getDocumentModuleName(), $directAccessModules))
        {
            $doc = $doc->getDocumentParent();
        }

        if(method_exists($doc, 'getEditUrl'))
        {
            $label = $docOrig->getLabel();
            
            // Get formatted label of original document            
            if(method_exists($docOrig, 'getBoSearchLabel'))
            {
                $label = $docOrig->getBoSearchLabel();
            }

            if(method_exists($docOrig, 'getBoSearchIcon'))
            {
                $img = $docOrig->getBoSearchIcon();
            }
            else
            {
                $img = m_medias_services_MediaService::getInstance()->getIconPath('page_white.png');
            }

            $label = '<img src='.$img.' alt="" title="" class="icon16" /> '.$label;

            echo $label.'|'.$doc->getEditUrl();

            if(!in_array($docOrig->getDocumentModuleName(), $directAccessModules))
            {
                echo '|'.$docOrig->getEditUrl();
            }

            echo "\n";
        }
    }

	public function errorHandler()
	{
		return null;
	}
}
